<template>
  <div class="container" v-loading="isLoading">
    <el-card>
      <template #header>
        <el-form inline ref="searchForm">
          <el-form-item label="轮播图类型" prop="news_type">
            <el-select v-model="builingForm.type_id" placeholder="请选择轮播图类型">
              <el-option label="全部" value=""/>
              <el-option
                v-for="(oVal, oIdx) of bannerType"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getList()">查询</el-button>
          </el-form-item>
        </el-form>
        <div class="table_tool">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="showAddOrEdit = true"
            >新增</el-button
          >
        </div>
      </template>

      <el-table :data="tableData" border>
        <el-table-column prop="name" label="描述" show-overflow-tooltip />
        <el-table-column prop="img" label="轮播图">
          <template slot-scope="{ row }">
            <el-image style="width: 200px; height: auto" :src="row.img" :preview-src-list="[row.img]" />
          </template>
        </el-table-column>
        <el-table-column prop="url" label="跳转地址" />
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              @click="openEditRow(row, 'builingForm', initImg)"
              >编辑</el-button
            >
            <el-button type="text" class="red" @click="deleteOrder(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="pagination.page_size"
        :total="tableTotal"
        @size-change="(val) => (pagination.page_size = val)"
        @current-change="(val) => (pagination.page = val)"
      />
    </footer>

    <el-dialog
      :title="(builingForm.$id ? '编辑' : '新增') + '轮播图'"
      :visible.sync="showAddOrEdit"
      width="1000px"
      @closed="resetForm()"
    >
      <el-form
        :model="builingForm"
        ref="builingForm"
        label-width="7em"
      >
        <el-form-item prop="$id" hidden />
        <el-form-item label="类型：" prop="news_type">
          <el-select v-model="builingForm.banners_type_id" placeholder="请选择类型">
            <el-option
              v-for="(oVal, oIdx) of bannerType"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="图片：" prop="img">
          <Upload
            v-model="builingForm.img"
            :file.sync="builingForm.img"
          />
        </el-form-item>
        <el-form-item label="跳转地址：" prop="url">
          <el-input
            v-model="builingForm.url"
            placeholder="请输入跳转地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述：" prop="name">
          <el-input
            v-model="builingForm.name"
            type="textarea"
            row="6"
            placeholder="请输入描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button
          type="primary"
          @click="submitRowData()"
          :loading="loading.update"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import API from '@/api';
import { table, addOrEdit } from "@/utils/mixins";
import Upload from "@/components/Upload/SingleImage.vue";
export default {
  components: {
    Upload,
  },
  mixins: [table, addOrEdit],
  data: () => ({
    bannerType:[
      {label:"首页",value:1},
      {label:"租房",value:2},
      {label:"招工",value:3},
    ],
    pagination: {
      page: 1, // 当前页码
      per_page: 10, // 每页条数
    },
    searchForm: {
      
    },
    builingForm: {
      $id: '',
      type_id:"",

      img:"",
      is_link:"",
      url: "",
      name:"",
      icon:"",
      banners_type_id:"",
    },
    builingRules: {},
  }),

  created:function(){
    this.getList()
  },

  methods: {
    // 获取列表
    getList(){
      this.loading=true
      API.getBannerList(this.builingForm.type_id).then(({data})=>{
        this.tableData=data
      })
    },

    // // 分页
    // changeSize(val){
    //   this.searchForm.per_page = val
    //   this.searchForm.page = 1
    //   this.getPermissionList()
    // },

    // // 分页
    // changeCurrent(val){
    //   this.searchForm.page = val
    //   this.getPermissionList()
    // },

    // 删除
    deleteOrder(id){
      let _that=this
      this.$confirm(`是否删除改轮播图？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
      .then(() => {
        API.deleteBanner(id)
        .then(({data}) => {
          this.$message.success("删除成功！");
        }).catch((err) => {
        }).finally(() =>{
          this.getList()
          _that.showAddOrEdit=false
        })
      })
      .catch(()=> {
      });
    },


    // 提交
    submitRowData(){
      let data={
        banners_type_id:this.builingForm.banners_type_id,
        img:this.builingForm.img,
        url:this.builingForm.url,
        is_link:this.builingForm.url?true:false,
        name:this.builingForm.name
      }
      if(!this.builingForm.banners_type_id) return this.$message.error("请选择轮播图类型~~");
      if(!this.builingForm.img.trim()) return this.$message.error("轮播图不可为空~~")

      if(this.builingForm.$id){
        let id=this.builingForm.$id
        API.editBanner(id,data)
        .then(({data}) => {
          this.$message.success("编辑成功！");
        }).catch((err) => {
        }).finally(() =>{
          this.resetForm()
          this.getList()
        })
      }else{
        API.addBanner(data)
          .then(({data}) => {
            this.$message.success("新增成功！");
          }).catch((err) => {
          }).finally(() =>{
            this.resetForm()
            this.getList()
          })
      }
    },
    
    // 重置弹窗
    resetForm(){
      this.builingForm.$id=""
      this.builingForm.type_id=""
      this.builingForm.img=""
      this.builingForm.is_link=false
      this.builingForm.url=""
      this.builingForm.name=""
      this.builingForm.banners_type_id="",
      this.showAddOrEdit=false
    },
  },
};
</script>

<style lang="scss" scoped>
  ::v-deep{
    .el-dialog{
      margin-top:10vh !important;
    }
    .el-switch{
      transform:translateY(6px)
    }
  }</style>
